import { WebSocketConnect } from '@commonProject/socket/socket'
import { print } from '@ve/utils/print'
import store from '@commonProject/store'
import isEmpty from 'lodash/isEmpty'

export default () => {
  //没有uid会跳else
  if (store.getters['user/uid'] && store.getters['fileConfig/data']['common']['websocketUrl']) {
    console.log('启动')
    //开启websockte
    WebSocketConnect({
      //URL建立
      url: store.getters['fileConfig/data']['common']['websocketUrl'],
      //默认open后发送数据
      isDefaultSendData: true,
      /*
    发送的数据，默认已经封装成响应式；如果后续有问题再调整
    数据格式有待确定
    */
      data: [],
      onSuccess(data) {
        if (data === '#ACK#') return
        let _data = JSON.parse(data)
        if (isEmpty(_data.data)) {
          return
        }
        let __data = JSON.parse(_data.data)
        console.log('websocket data', __data)
        if (__data.SKT_METHOD == 'logout') {
          //退出
          store.dispatch('user/logout')
        } else {
          //打印
          try {
            const jsonParse = JSON.parse(data)
            if (jsonParse.data) {
              print([JSON.parse(jsonParse.data)], {
                $store: store,
              })
            }
          } catch (error) {
            console.log(error)
          }
        }
      },
    })
  } else {
    console.log('没有配置websockeUrl 不启动')
  }
}
